package com.csun.dsms.service.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.csun.dsms.comment.core.utils.wrapper.WrapMapper;
import com.csun.dsms.comment.core.utils.wrapper.Wrapper;
import com.csun.dsms.mapper.JyReminderDataMapper;
import com.csun.dsms.model.education.JyProblemManagement;
import com.csun.dsms.model.education.JyReminderData;
import com.csun.dsms.service.JyReminderDataService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class JyReminderDataServiceImpl implements JyReminderDataService {

    @Resource
    private JyReminderDataMapper jyReminderDataMapper;

    @Override
    public Wrapper<Object> addReminder(JyProblemManagement jyProblemManagement) {
        try {
            JyReminderData jyReminderData = new JyReminderData();
            Date date = new Date();
            jyReminderData.setReminderPerson(jyProblemManagement.getReportUser());
            jyReminderData.setCreatedTime(date);
            jyReminderData.setReminderDepart(jyProblemManagement.getDepartment());
            jyReminderData.setReminderPhoneNumber(jyProblemManagement.getReportUserPhone());
            jyReminderData.setReminderTime(date);
            jyReminderData.setReminderType(1);
            jyReminderData.setReminderContent(jyProblemManagement.getReportUser() + "有新增办件，请注意！");
            jyReminderData.setReminderTitle(jyProblemManagement.getReportUser() + "有新增办件，请注意！");
            jyReminderDataMapper.insertSelective(jyReminderData);
            return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE, "添加提醒成功");
        } catch (Exception e) {
            log.error("添加提醒失败", e);
            return WrapMapper.wrap(Wrapper.ERROR_CODE, e.getMessage());
        }
    }

    @Override
    public Wrapper<Object> queryReminderData(JSONObject reqJson) {
        try {
            Integer pageNum = reqJson.getInteger("pageNum");
            Integer pageSize = reqJson.getInteger("pageSize");
            if (pageNum == null || pageSize == null) {
                return WrapMapper.error("页码和每页数量不能为空");
            }
            PageHelper.startPage(pageNum, pageSize);
            JyReminderData jyReminderData = new JyReminderData();
            if (StrUtil.isNotBlank(reqJson.getString("reminderType"))) {
                jyReminderData.setReminderType(reqJson.getInteger("reminderType"));
            }
            List<JyReminderData> jyReminderDataList = jyReminderDataMapper.select(jyReminderData);
            PageInfo<JyReminderData> jyReminderDataPageInfo = new PageInfo<>(jyReminderDataList);
            return WrapMapper.ok(jyReminderDataPageInfo);
        } catch (Exception e) {
            log.error("查询失败", e);
            return WrapMapper.wrap(Wrapper.ERROR_CODE, e.getMessage());
        }
    }

}
